<template>
	<!-- 头部轮播 -->
	<view class="carousel-section">
		<!-- 标题栏和状态栏占位符 -->
		<view class="titleNview-placing"></view>
		<!-- 背景色区域 -->
		<view class="titleNview-background" :style="{backgroundColor:titleNViewBackground}"></view>
		<swiper class="carousel" circular @change="swiperChange">
			<swiper-item v-for="(item, index) in carouselList" :key="index" class="carousel-item" @click="navToDetailPage(item)">
				<image class="carousel-item-img" :src="item.src" />
			</swiper-item>
		</swiper>
		<!-- 自定义swiper指示器 -->
		<view class="swiper-dots">
			<text class="num">{{swiperCurrent+1}}</text>
			<text class="sign">/</text>
			<text class="num">{{swiperLength}}</text>
		</view>
	</view>
</template>

<script>
	export default {
		name: 'uni-swipe',
		data() {
			return {
				titleNViewBackground: '',
				swiperCurrent: 0,
				swiperLength: 0,
				carouselList: []
			};
		},

		mounted() {
			this.loadData();
		},
		methods: {
			/**
			 * 请求静态数据只是为了代码不那么乱
			 * 分次请求未作整合
			 * TODO: 必不可少的两个属性: { background: "rgb(203, 87, 60)", src: "/static/temp/banner3.jpg"}
			 */
			async loadData() {
				let carouselList = await this.$api.json('carouselList');
				this.titleNViewBackground = carouselList[0].background;
				this.swiperLength = carouselList.length;
				this.carouselList = carouselList;
			},
			//轮播图切换修改背景色
			swiperChange(e) {
				const index = e.detail.current;
				this.swiperCurrent = index;
				this.titleNViewBackground = this.carouselList[index].background;
			},
			//详情页
			navToDetailPage(item) {
				uni.navigateTo({
					// toast提醒太强了 - 后期关闭
					url: `/pages/product/product?id=${111}`
				})
			},
		}
	}
</script>

<style lang="scss">
	/* 头部 轮播图 */
	.carousel-section {
		position: relative;
		padding-top: 10px;

		// 标题栏和状态栏占位符
		.titleNview-placing {
			height: var(--status-bar-height);
			padding-top: 44px;
			box-sizing: content-box;
		}

		// 背景色
		.titleNview-background {
			position: absolute;
			top: 0;
			left: 0;
			width: 100%;
			height: 426upx;
			transition: .4s;
		}
	}

	// swiper组件
	.carousel {
		width: 100%;
		height: 350upx;

		.carousel-item {
			width: 100%;
			height: 100%;
			padding: 0 28upx;
			overflow: hidden;
		}

		.carousel-item-img {
			width: 100%;
			height: 100%;
			border-radius: 10upx;
		}
	}

	// 指示器
	.swiper-dots {
		display: flex;
		position: absolute;
		left: 60upx;
		bottom: 15upx;
		width: 72upx;
		height: 36upx;
		background-image: url();
		background-size: 100% 100%;

		.num {
			width: 36upx;
			height: 36upx;
			border-radius: 50px;
			font-size: 24upx;
			color: #fff;
			text-align: center;
			line-height: 36upx;
		}

		.sign {
			position: absolute;
			top: 0;
			left: 50%;
			line-height: 36upx;
			font-size: 12upx;
			color: #fff;
			transform: translateX(-50%);
		}
	}

	/* -------------------------------- */

	// 小程序特有,不显示背景色
	/* #ifdef MP */
	page {
		.cate-section {
			position: relative;
			z-index: 5;
			border-radius: 16upx 16upx 0 0;
			margin-top: -20upx;
		}

		.carousel-section {
			padding: 0;

			// 标题栏和状态栏占位符 - 小程序不占位
			.titleNview-placing {
				padding-top: 0;
				height: 0;
			}

			// swiper组件
			.carousel {
				.carousel-item {
					padding: 0;
				}
			}

			// 指示器
			.swiper-dots {
				left: 45upx;
				bottom: 40upx;
			}
		}
	}

	/* #endif */
</style>
